package com.michael.demos.springboot.ext.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/3 15:47
 */
@Slf4j
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {

        if (applicationEvent instanceof ContextClosedEvent) {
            log.error("Spring 容器关闭了...");
        } else if (applicationEvent instanceof ContextStartedEvent) {
            log.error("Spring 容器启动了...");
        } else if (applicationEvent instanceof ContextRefreshedEvent) {
            log.error("Spring 容器刷新了...");
        } else if (applicationEvent instanceof ContextStoppedEvent) {
            log.error("Spring 容器停止了...");

        } else if (applicationEvent instanceof ServletWebServerInitializedEvent) {
            log.error("应用运行中...");
        } else if (applicationEvent instanceof ApplicationStartedEvent) {
            log.error("应用启动完成...");
        } else if (applicationEvent instanceof ApplicationReadyEvent) {
            log.error("应用完成初始化...");
        } else {
            log.error("接收到事件 {}", applicationEvent);
        }
    }
}
